package com.chidopi.market.bookstore.dao.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import com.chidopi.cdf.dao.AbstractBaseDaoImpl;
import com.chidopi.market.bookstore.domain.BookCategories;

public class BookCategoriesDaoImpl extends AbstractBaseDaoImpl<BookCategories>  {
	
	private static Logger logger = LoggerFactory.getLogger(BookCategoriesDaoImpl.class);

	@Override
	public int insert(BookCategories t) {
		String sql = "insert into bookcategories (CATEGORYNAME, CREATE_DATE, CREATE_BY) "
				+ "values (:categoryname, NOW(), :create_by)";
		return super.execSQL(sql, t);
	}

	@Override
	public int update(BookCategories t) {
		String sql = "update bookcategories set CATEGORYNAME = :categoryname, update_by = :update_by where id= :id";
		return super.execSQL(sql, t);
	}

	@Override
	public int delete(BookCategories t) {
		String sql = "delete from bookcategories where id = :id";
		return super.execSQL(sql, t);
	}

	@Override
	public List<BookCategories> find(String sqlWhere, Object... args) {
		if (sqlWhere == null || sqlWhere.trim().equals("")) {
			String sql = "select * from bookcategories order by id ";
			return super.simpleJdbcTemplate.query(sql, this.getRowMapper());
		} else {
			logger.debug("sqlWhere:[" + sqlWhere + "]/params:" + Arrays.asList(args).toString());
			String sql = "select * from bookcategories where " + sqlWhere;
			List<BookCategories> list = super.simpleJdbcTemplate.query(sql, this.getRowMapper(), args);
			if (list == null) {
				logger.debug("query null!!!");
			} else {
				for (BookCategories category : list) {
					logger.debug("id=" + category.getId() + "/categoryname=" + category.getCategoryname());
				}
			}
			return list;
		}
	}

	@Override
	public BookCategories findById(Object id) {
		String sql = "id=?";
		List<BookCategories> list = this.find(sql, id);
		if (0 == list.size()) return null;
		return list.get(0);
	}

	@Override
	protected String getTable() {
		return "bookcategories";
	}

	@Override
	protected String getSequenceName() {
		return null;
	}

	@Override
	protected RowMapper<BookCategories> getRowMapper() {
		return ParameterizedBeanPropertyRowMapper.newInstance(BookCategories.class);
	}

}
